package com.nic.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.nic.view.R;
import com.nic.view.util.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2018/8/28.
 */

public class RecycleHorizontalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<String> urlList = new ArrayList<>();
    private Context context;

    public RecycleHorizontalAdapter(Context context) {
        this.context = context;
    }

    public void setUrlList(List<String> urlList) {
        if (urlList != null) {
            this.urlList = urlList;
        }
        notifyDataSetChanged();
    }

    public List<String> getUrlList() {
        return urlList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            final String url = urlList.get(position);
            if (!TextUtils.isEmpty(url)) {
                GlideUtil.getInstance().load(url, viewHolder.ivImage);
            } else {
                viewHolder.ivImage.setImageResource(R.drawable.ic_main_null);

            }
            viewHolder.ivImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onImageClickListener != null) {
                        onImageClickListener.onClick(v, url);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return urlList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivImage;

        ViewHolder(View view) {
            super(view);
            ivImage = (ImageView) view.findViewById(R.id.iv_image);
        }
    }

    private OnImageClickListener onImageClickListener;

    public void setOnImageClickListener(OnImageClickListener onImageClickListener) {
        this.onImageClickListener = onImageClickListener;
    }

    public interface OnImageClickListener {
        void onClick(View v, String url);
    }
}
